/**
 * Copyright (c) 2009 Blue Whale Systems Ltd
 * 
 * This software is provided "as is," and the copyright holder makes no representations or warranties, express or
 * implied, including but not limited to warranties of merchantability or fitness for any particular purpose or that the
 * use of this software or documentation will not infringe any third party patents, copyrights, trademarks or other
 * rights.
 * 
 * The copyright holder will not be liable for any direct, indirect special or consequential damages arising out of any
 * use of this software or documentation.
 */
package com.bluewhalesystems.client.lwuit.richtext;

import java.io.OutputStream;

import junit.framework.TestCase;

public final class GrowableCharArrayTest extends TestCase
{
    public void testCapacityChanges()
    {
        System.out.println( "testCapacityChanges" );

        try
        {
            // Create an instance with a small initial capacity and a small increment.
            GrowableCharArray content = new GrowableCharArray();
            content.reset( 5, 4 );
            content.append( 'a' );
            content.append( 'b' );
            content.append( 'c' );
            content.append( "defg" );

            // One increment should have occured.
            assertTrue( "0. capacity is 9", 9 == content.capacity() );
            assertTrue( "0. length is 7", 7 == content.length() );

            // Trim to length.
            content.trim();

            assertTrue( "1. capacity is 7", 7 == content.capacity() );
            assertTrue( "1. content is 'abcdefg'", ( new String( content.getArray() ) ).equals( "abcdefg" ) );

            // Clear the content, which should not affect capacity.
            content.clear();

            assertTrue( "2. length is 0", 0 == content.length() );
            assertTrue( "2. capacity is 7", 7 == content.capacity() );

            // Add a longer content.        
            int expectedLength = STRINGS[0].length();
            content.append( STRINGS[0] );

            assertTrue( "3. length is " + expectedLength, expectedLength == content.length() );

            // Reserve a much larger capacity which should force another copy.
            int newCapacity = 20 * 1024;
            content.ensureCapacity( newCapacity );

            assertTrue( "4. length is " + expectedLength, expectedLength == content.length() );
            assertTrue( "4. capacity is " + newCapacity, newCapacity == content.capacity() );

            // Trim again.
            content.trim();

            assertTrue( "5. length is " + expectedLength, expectedLength == content.length() );
            assertTrue( "5. capacity is " + expectedLength, expectedLength == content.capacity() );
        }
        catch( Exception e )
        {
            e.printStackTrace();
            fail();
        }
    }

    public void testNormalOperation()
    {
        System.out.println( "testNormalOperation" );

        try
        {
            // Create an instance with default initial capacity and increment.
            GrowableCharArray content = new GrowableCharArray();

            // Allocate a larger amount.
            int newCapacity = 64 * 1024;
            content.ensureCapacity( newCapacity );

            assertTrue( "0. capacity is " + newCapacity, newCapacity == content.capacity() );

            // Add some content, verify capacity has not changed.
            content.append( STRINGS[0] );

            assertTrue( "1. capacity is " + newCapacity, newCapacity == content.capacity() );

            // Add some content, verify capacity has not changed.
            content.append( STRINGS[1] );

            assertTrue( "2. capacity is " + newCapacity, newCapacity == content.capacity() );

            // Trim.
            content.trim();
            int expectedLength = STRINGS[0].length() + STRINGS[1].length();

            assertTrue( "3. length is " + expectedLength, expectedLength == content.length() );
            assertTrue( "3. capacity is " + expectedLength, expectedLength == content.capacity() );

            // Clear.
            content.clear();

            assertTrue( "4. length is 0", 0 == content.length() );
            assertTrue( "4. capacity is " + expectedLength, expectedLength == content.capacity() );

            // Trim a cleared content which should free up all the allocated capacity.
            content.trim();

            assertTrue( "5. length is 0", 0 == content.length() );
            assertTrue( "5. capacity is 0", 0 == content.capacity() );
        }
        catch( Exception e )
        {
            e.printStackTrace();
            fail();
        }
    }

    private static final String[] STRINGS = {
            "This software is provided 'as is,' and the copyright holder makes no representations or warranties, express or implied, including but not limited "
                    + "to warranties of merchantability or fitness for any particular purpose or that the use of this software or documentation will not infringe any "
                    + "third party patents, copyrights, trademarks or other rights.",
            "If the current capacity of this vector is less than minCapacity, then its capacity is increased by replacing its internal data array, kept in "
                    + "the field elementData, with a larger one. The size of the new data array will be the old size plus capacityIncrement, unless the value of "
                    + "capacityIncrement is less than or equal to zero, in which case the new capacity will be twice the old capacity; but if this new size is still "
                    + "smaller than minCapacity, then the new capacity will be minCapacity." };
}
